﻿using System;
using System.Linq;
using OS.Infrastructure.Common;
using OS.Infrastructure.Domain.UnitOfWork;
using TaskKernel.BootStrapper;
using TaskKernel.Domain.Interface;
using TaskKernel.Domain.Services.Mail;

namespace WebTasksTimerConsole
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            TaskKernelBootStrapper.RunConsoleApp();
            var dateTime = DateTime.Now;
            using (var z = IoC.Resolve<IUnitOfWorkFactory>().Create()){
                RequestCloseTasksWithTimeCreate(dateTime);
                UnclosedTasksWithExpireReminder(dateTime);
            }
        }

        private static void RequestCloseTasksWithTimeCreate(DateTime dateTime)
        {
            var messageProcessor = IoC.Resolve<IMessageService>();
            var tasks = IoC.Resolve<IQueryTask>().RequestCloseTasksWithTimeCreate(dateTime);
            if (!tasks.Any()) return;
            foreach (var task in tasks)
                messageProcessor.RememberCloseNotification(task);
        }

        private static void UnclosedTasksWithExpireReminder(DateTime dateTime)
        {
            var messageProcessor = IoC.Resolve<IMessageService>();
            var tasks = IoC.Resolve<IQueryTask>().UnclosedTasksWithExpireReminder(dateTime);
            if (!tasks.Any()) return;
            foreach (var task in tasks){
                var reminders = task.GetExpirationRemindersByTime(dateTime);
                foreach (var reminder in reminders)
                    messageProcessor.ReminderNotification(reminder);
            }
        }
    }
}